﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kooboo.IoC;
using Kooboo.Domain;

namespace Kooboo.Commerce
{
    public static class OrderCommand
    {
        public static void ChangeStatus(this IOrder order,OrderStatus status)
        {
            if (order == null)
            {
                throw new ArgumentNullException("order");
            }

            var service = ContextContainer.Current.Resolve<IOrderRepository>();

            var find = service.Orders.Where(i => i.Id == order.Id).FirstOrDefault();

            if (find == null)
            {
                throw new KeyNotFoundException("OrderId:" + order.Id);
            }

            if (find.OrderStatus != status)
            {
                using (var transcation = ObjectContainer.CreateInstance<TransactionContext>())
                {
                    find.OrderStatus = status;
                    service.Put(find);
                    transcation.Commit();
                }
            }
        }

        public static void UpdateRemark(this IOrder order, string remark)
        {
            if (order == null)
            {
                throw new ArgumentNullException("order");
            }

            var service = ContextContainer.Current.Resolve<IOrderRepository>();

            var find = service.Orders.Where(i => i.Id == order.Id).FirstOrDefault();

            if (find == null)
            {
                throw new KeyNotFoundException("OrderId:" + order.Id);
            }

         
            using (var transcation = ObjectContainer.CreateInstance<TransactionContext>())
            {
                find.Remark = remark;
                service.Put(find);
                transcation.Commit();
            }
           
        }

        //public static void Rate(this IOrder order, IOrderItem item, StarLevel stars, string remark)
        //{
            
        //    var review = ObjectContainer.CreateInstance<IProductReview>();

        //    review.CreateDate = DateTime.Now;
        //    review.IsPublished = false;
        //  //  review.Product = item.Product;
        //    review.Rate = stars;
        //    review.Remark = remark;
        //    review.Order = item.Order;


        //    PromotionContext context = new PromotionContext();

        //    context.Status = CheckoutStatus.Feedback;

        //    ServiceFactory
        //         .GetService<OrderService>()
        //         .UpdateTotal(review.Order);

        //    using (var transaction = ObjectContainer.CreateInstance<TransactionContext>())
        //    {
        //        try
        //        {
        //            ContextContainer.Current.Resolve<IProductReviewRepository>()
        //                .Post(review);

        //            ContextContainer.Current.Resolve<IOrderRepository>()
        //                .Put(order);

        //            transaction.Commit();
        //        }
        //        catch
        //        {
        //            transaction.Rollback();
        //            throw;
        //        }
        //    }

        //}
    }
}
